
#import "SpiderDrawDelegate.h"
#import "../Solitaire/CardSet.subproj/cardset.h"
#import "localstrings.h"

@implementation SpiderDrawDelegate

- setGameCardPileViews:(id *)theViews
{
    int i;
    for (i = 0; i < 10; i++)
    {
        gameCardPiles[i] = theViews[i];
    }
    return self;
}

- clickedCard:aCard in:drawCardPileView
{
    id    drawCardPile = [drawCardPileView cardPile];
    id    drawCard;
    int   i;
    int   numSpaces;
    int   numCards;
    
    if (aCard)
    {
        numSpaces = 0;
	for (i = 0; i < 10; i++)
	{
	    if ([[gameCardPiles[i] cardPile] cardCount] == 0) numSpaces++;
	}
	
	if (numSpaces > 0)
	{
	    numCards = 0;
	    for (i = 0; i < 10; i++)
	    {
	        numCards += [[gameCardPiles[i] cardPile] cardCount];
	    }
	    
	    if (numCards >= 10)
	    {
	        NXRunAlertPanel(LOCALIZED_GAME_NAME, LOCALIZED_FILL_SPACES,
		                NULL, NULL, NULL);
	        return self;
	    }
	}
	[[drawCardPileView window] disableFlushWindow];
	for (i = 0; i < 10; i++)
	{
	    drawCard = [drawCardPile cardAt:CS_TOP];
	    [drawCardPile removeCard:drawCard];
	    [drawCard flip];
	    [[gameCardPiles[i] cardPile]addCard:drawCard];
	    [drawCardPileView display];
	    [gameCardPiles[i] display];
	}
	[[drawCardPileView window] reenableFlushWindow];
	[[drawCardPileView window] flushWindow];
    }
    return self;
}

@end
